from django.db import models
from users.models import User


class NewsCategory(models.Model):
    # 新闻类名
    id = models.IntegerField(primary_key=True, unique=True, verbose_name='新闻id')
    name = models.CharField(max_length=10, unique=True, verbose_name='新闻类别')

    class Meta:
        db_table = 'tb_newscategory'
        verbose_name = '新闻类别'
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __str__(self):
        return self.name


# 新闻
class News(models.Model):
    'news tables'

    # 新闻状态枚举数据
    status_enum = (
        (0, '待审核'),
        (1, '审核通过'),
    )

    # 新闻标题
    title = models.CharField(max_length=50,verbose_name='头条标题')
    # 新闻来源
    source = models.CharField(max_length=100,verbose_name='新闻来源')
    # 新闻默认图片
    # default_image = models.CharField(max_length=100,unique=True,verbose_name='新闻图片')
    default_image = models.CharField(max_length=100,null=True,verbose_name='新闻图片')
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')
    # 新闻摘要
    digest = models.CharField(max_length=50, null=True,verbose_name='新闻摘要')
    # 新闻点击量
    clicks = models.IntegerField(null=True,verbose_name='新闻点击量')
    # 新闻详情
    content = models.TextField(max_length=1000,verbose_name='新闻详情')
    # 新闻类别
    category_id = models.ForeignKey(NewsCategory, on_delete=models.CASCADE, verbose_name='新闻类别')
    # 用户id
    user_id = models.ForeignKey(User,verbose_name='用户id')
    # 新闻审核状态
    status = models.SmallIntegerField(choices=status_enum, default=0, verbose_name='审核状态')

    class Meta:
        db_table = 'tb_News'
        verbose_name = '新闻'
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.id)


# 评论
class Comment(models.Model):
    # 'comment tables'

    user_id = models.ForeignKey(User,verbose_name='用户id')
    new_id = models.ForeignKey(News,verbose_name='评论的新闻')
    # 评论时间
    comment_create_time = models.DateTimeField(auto_created=True, verbose_name='评论时间')
    # 评论内容
    comment_content = models.CharField(max_length=100, null=False,verbose_name='评论内容')
    # 评论者id
    # praent_id =  models.ForeignKey(User,verbose_name='回复评论id')
    # 点赞数
    like_count = models.IntegerField(unique=True, verbose_name='点赞次数')

    class Meta:
        db_table = 'tb_commment'
        verbose_name = '评论'
        verbose_name_plural = verbose_name




# 点赞
class Comment_like(models.Model):

    user_id = models.ForeignKey(User,verbose_name='点赞用户')

    class Meta:
        db_table = 'tb_comment_like'
        verbose_name = '点赞表'
        verbose_name_plural = verbose_name


# 新闻广告类别
class NewContentCategory(models.Model):
    """
    广告内容类别
    :param 广告类别
    """
    name = models.CharField(max_length=50, verbose_name='名称')
    key = models.CharField(max_length=50, verbose_name='类别键名')

    class Meta:
        db_table = 'tb_New_content_category'
        verbose_name = '广告内容类别'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 新闻广告
class NewContent(models.Model):
    """
    广告内容
    """
    category = models.ForeignKey(NewContentCategory, on_delete=models.PROTECT, verbose_name='类别')
    title = models.CharField(max_length=100, verbose_name='标题')
    url = models.CharField(max_length=300, verbose_name='内容链接')
    image = models.ImageField(null=True, blank=True, verbose_name='图片')
    text = models.TextField(null=True, blank=True, verbose_name='内容')
    sequence = models.IntegerField(verbose_name='排序')
    status = models.BooleanField(default=True, verbose_name='是否展示')

    class Meta:
        db_table = 'tb_New_content'
        verbose_name = '广告内容'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title

